package com.webledger.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import com.webledger.db.DBClass;
import com.webledger.userclass.UInfo;

public class LoginAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String username, password, Query;
	int userID = 0;
	SessionMap<String, Object> sessionmap;
	Connection con = DBClass.getDB().getConnection();
	ResultSet rsVoucher;
	private String ledgervoucherType;
	private ArrayList<VoucherType> vouchertype;
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();

	public String defalutVoucherType() {
		return "Debit";
	}

	public String getLedgervoucherType() {
		return ledgervoucherType;
	}

	public void setLedgervoucherType(String ledgervoucherType) {
		this.ledgervoucherType = ledgervoucherType;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getSessioID() {
		if (((Integer) session.getAttribute("userid")) != null) {
			userID = ((Integer) session.getAttribute("userid"));

		}

		return userID;
	}

	public String execute() {

		Query = "select usercode,username,password from user where username='"
				+ username + "' and password='" + password + "'";
		String str = "";
		PreparedStatement ps;
		// userID = getSessioID();
		// uinfo.removeSessionId("userid");

		try {
			/*
			 * if (userID != 0) { addActionError("Alerady Login"); str
			 * ="success";
			 * 
			 * } else {
			 */
			/*
			 * if (!username.equalsIgnoreCase("") &&
			 * !password.equalsIgnoreCase("")) {
			 */

			ps = con.prepareStatement(Query);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				userID = rs.getInt(1);
				sessionmap.put("userid", userID);
				GetVoucherType();

				str = "success";
			} else {
				addActionError("Invalid Username/Password");
				str = "error";

			}
			/*
			 * } else { str = "error"; //}
			 */
			// }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addActionError("Exception Occured");
			return "error";
		}
		return str;

	}

	public void GetVoucherType() {
		String VoucherQuery = "select VoucherTypeNo,VoucherTypeName from VoucherType order by VoucherTypeName";
		try {
			PreparedStatement psVoucher = con.prepareStatement(VoucherQuery);
			rsVoucher = psVoucher.executeQuery();
			vouchertype = new ArrayList<VoucherType>();
			while (rsVoucher.next()) {
				vouchertype.add(new VoucherType(rsVoucher.getString(2),
						rsVoucher.getInt(1)));
			}
			rsVoucher.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch bloc
			e.printStackTrace();
		}

	}
	public ArrayList<VoucherType> getVouchertype() {
		return vouchertype;
	}

	public void setVouchertype(ArrayList<VoucherType> vouchertype) {
		this.vouchertype = vouchertype;
	}

	public void setSession(Map<String, Object> map) {
		sessionmap = (SessionMap) map;

	}

}
